<?php
namespace Admin\Controller;
use Admin\Builder\AdminConfigBuilder;

class ActmanageController extends AdminController{
		public function index(){
			$this->display();
		}
    public function vipindex(){
			$dem = M('TbDemand');
			$xname=I('xname','','text');
			$map['xstatus'] = array('eq', 0);
			//$map['xdstatus']=array('elt',0);
				
			/* if ($xname !== '') {
			 $map['xname'] = array('like', '%' . (string)$xname . '%');
			} */
			 $sql='SELECT duijie_tb_demand.*,duijie_tb_tecdem.id tttids,duijie_tb_tecdem.fplace,duijie_tb_tecdem.place FROM duijie_tb_demand,duijie_tb_tecdem
			 WHERE  duijie_tb_demand.rid = duijie_tb_tecdem.id and duijie_tb_demand.xstatus=0';
			$list=$dem->where($map)->query($sql); 
			/* $list=$dem->join('duijie_tb_tecdem on duijie_tb_demand.rid = duijie_tb_tecdem.id')
			->where($map)
			->select(); */
			//dump($list);
			//$list=$dem->select();
			foreach ($list as $key => $value) {
				$str=$list[$key]['jsbq'];
				$mapp['id']=array('in',$str);
				$list[$key]['tids']=M("TbLable")->where($mapp)->select();
			}
		
			foreach ($list as $key => $value) {
				$str1=$list[$key]['fplace'];
				$str2=$list[$key]['place'];
				$map3['id']=$str1;
				$map4['id']=$str2;
				$list[$key]['tids1']=M("District")->where($map3)->select();
				$list[$key]['tids2']=M("District")->where($map4)->select();
			}
			int_to_string($list,array('xstatus'=>array(0=>'未评估',1=>'评估中',2=>'服务完成')));              //数字转为字符串
			// 			$list = $dem->join('duijie_tb_tecdem on duijie_tb_demand.rid=duijie_tb_tecach.id')
			// 			->select();
			$this->assign('_list', $list);
			
			$this->display();
		}
		
		public function changeStatus($id){
			
				$demand    = M('TbDemand');
				
				$map['xstatus'] = 1;
				$map['id'] = $id;
				$rst = $demand->save($map);
				//dump($id);
				if($rst === false){
					$this->error("出错了");
				}
			
		}
		
		public function viping(){
			$dem = M('TbDemand');
			$xname=I('xname','','text');
			$map['xstatus'] = array('eq', 1);
			//$map['xdstatus']=array('elt',0);
				
			/* if ($xname !== '') {
			 $map['xname'] = array('like', '%' . (string)$xname . '%');
			} */
			 $sql='SELECT duijie_tb_demand.*,duijie_tb_tecdem.id tttids,duijie_tb_tecdem.fplace,duijie_tb_tecdem.place FROM duijie_tb_demand,duijie_tb_tecdem
			 WHERE  duijie_tb_demand.rid = duijie_tb_tecdem.id and duijie_tb_demand.xstatus=1';
			$list=$dem->where($map)->query($sql); 
			/* $list=$dem->join('duijie_tb_tecdem on duijie_tb_demand.rid = duijie_tb_tecdem.id')
			->where($map)
			->select(); */
			//dump($list);
			//$list=$dem->select();
			foreach ($list as $key => $value) {
				$str=$list[$key]['jsbq'];
				$mapp['id']=array('in',$str);
				$list[$key]['tids']=M("TbLable")->where($mapp)->select();
			}
		
			foreach ($list as $key => $value) {
				$str1=$list[$key]['fplace'];
				$str2=$list[$key]['place'];
				$map3['id']=$str1;
				$map4['id']=$str2;
				$list[$key]['tids1']=M("District")->where($map3)->select();
				$list[$key]['tids2']=M("District")->where($map4)->select();
			}
			int_to_string($list,array('xstatus'=>array(0=>'未评估',1=>'评估中',2=>'服务完成')));              //数字转为字符串
			// 			$list = $dem->join('duijie_tb_tecdem on duijie_tb_demand.rid=duijie_tb_tecach.id')
			// 			->select();
			$this->assign('_list', $list);
			//dump($list);
			$this->display();
		}
		
		/* public function pgjs(){
			$dem = M('TbDemand');
			$xname=I('xname','','text');
			$map['xstatus'] = array('eq', 2);
			
			$sql='SELECT duijie_tb_demand.*,duijie_tb_tecdem.id tttids,duijie_tb_tecdem.fplace,duijie_tb_tecdem.place FROM duijie_tb_demand,duijie_tb_tecdem
			 WHERE  duijie_tb_demand.rid = duijie_tb_tecdem.id and duijie_tb_demand.xstatus=2';
			$list=$dem->where($map)->query($sql);
			
			foreach ($list as $key => $value) {
				$str=$list[$key]['jsbq'];
				$mapp['id']=array('in',$str);
				$list[$key]['tids']=M("TbLable")->where($mapp)->select();
			}
		
			foreach ($list as $key => $value) {
				$str1=$list[$key]['fplace'];
				$str2=$list[$key]['place'];
				$map3['id']=$str1;
				$map4['id']=$str2;
				$list[$key]['tids1']=M("District")->where($map3)->select();
				$list[$key]['tids2']=M("District")->where($map4)->select();
			}
			int_to_string($list,array('xstatus'=>array(0=>'未评估',1=>'评估中',2=>'评估结束',3=>'报表已上传')));              //数字转为字符串
			
			$this->assign('_list', $list);
			
			$this->display();
		} */
		
		public function fwwc(){
			$dem = M('TbDemand');
			$xname=I('xname','','text');
			$map['xstatus'] = array('eq', 2);
			//$map['xdstatus']=array('elt',0);
		
			/* if ($xname !== '') {
			 $map['xname'] = array('like', '%' . (string)$xname . '%');
			} */
			 $sql='SELECT duijie_tb_demand.*,duijie_tb_tecdem.id tttids,duijie_tb_tecdem.fplace,duijie_tb_tecdem.place FROM duijie_tb_demand,duijie_tb_tecdem
			 WHERE  duijie_tb_demand.rid = duijie_tb_tecdem.id and duijie_tb_demand.xstatus=2';
			$list=$dem->where($map)->query($sql); 
			/* $list=$dem->join('duijie_tb_tecdem on duijie_tb_demand.rid = duijie_tb_tecdem.id')
			->where($map)
			->select(); */
			//dump($list);
			//$list=$dem->select();
			foreach ($list as $key => $value) {
				$str=$list[$key]['jsbq'];
				$mapp['id']=array('in',$str);
				$list[$key]['tids']=M("TbLable")->where($mapp)->select();
			}
		
			foreach ($list as $key => $value) {
				$str1=$list[$key]['fplace'];
				$str2=$list[$key]['place'];
				$map3['id']=$str1;
				$map4['id']=$str2;
				$list[$key]['tids1']=M("District")->where($map3)->select();
				$list[$key]['tids2']=M("District")->where($map4)->select();
			}
			int_to_string($list,array('xstatus'=>array(0=>'未评估',1=>'评估中',2=>'服务完成')));              //数字转为字符串
			// 			$list = $dem->join('duijie_tb_tecdem on duijie_tb_demand.rid=duijie_tb_tecach.id')
			// 			->select();
			$this->assign('_list', $list);
			//dump($list);
			$this->display();
		}
		
		public function xqdetail(){
				$did=I('id');
// 				dump($did);
				$demand	=	M('TbDemand');
                
				/* $sql='SELECT * from duijie_tb_tecdem,duijie_tb_demand,duijie_tb_teccoo,duijie_tb_teccsd
					WHERE duijie_tb_tecdem.id=duijie_tb_demand.rid
					AND duijie_tb_demand.cooperation=duijie_tb_teccoo.id
					AND duijie_tb_teccsd.id=duijie_tb_demand.csd;'; */
			//	$data=$demand->select($id);
			   // $data=$demand->query($sql);
			   $where['duijie_tb_demand.id']=$did;
				$data=$demand->join('duijie_tb_tecdem on duijie_tb_tecdem.id=duijie_tb_demand.rid')
				->join('duijie_tb_teccoo on duijie_tb_teccoo.cid=duijie_tb_demand.cooperation')
				->join('duijie_tb_teccsd on duijie_tb_teccsd.sid=duijie_tb_demand.csd')
// 				->field('duijie_tb_teccoo.*,duijie_tb_demand.id idsss')
				->where($where)
				->select();
				 
				int_to_string($data,array('xdstatus'=>array(-1=>'未对接',0=>'对接中',1=>'对接已结束')));              //数字转为字符串
// 				dump($data);
			    foreach ($data as $key => $value) {
			    	$str=$data[$key]['fplace'];
			    	$str2=$data[$key]['place'];
			    	$map['id']=$str;
			    	$map2['id']=$str2;
			    	$data[$key]['tids']=M("District")->where($map)->select();
			    	$data[$key]['tids2']=M("District")->where($map2)->select();
			    }
			    
			    foreach ($data as $key => $value) {
			    	$str3=$data[$key]['ffield'];
			    	$str4=$data[$key]['field'];
			    	$map3['ID']=$str3;
			    	$map4['ID']=$str4;
			    	$data[$key]['tids3']=M("TbTeclab")->where($map3)->select();
			    	$data[$key]['tids4']=M("TbTeclab")->where($map4)->select();
			    }
			    $this->assign('data', $data);
// 			    dump($data);
			    
				$this->display();
			}
			

			public function jsdetail(){
				$id=I('id');
				$tec=M('TbTecach');
				$map['duijie_tb_tecach.id']=$id;
				$map['duijie_tb_tecach.duijie']=array('eq',0);
				$data=$tec->join('duijie_tb_techol on duijie_tb_techol.id=duijie_tb_tecach.cID')
				->join('duijie_tb_teccsd on duijie_tb_teccsd.sid=duijie_tb_tecach.csd')
				->join('duijie_tb_teccoo on duijie_tb_teccoo.cid=duijie_tb_tecach.hztype')
				->join('duijie_tb_patent on duijie_tb_patent.id=duijie_tb_tecach.patentid',left)
				->join('duijie_tb_award on duijie_tb_award.id=duijie_tb_tecach.awardid',left)
				->join('duijie_tb_paper on duijie_tb_paper.id=duijie_tb_tecach.paperid',left)
				->where($map)
				->select();
				
				foreach ($data as $key => $value) {
					$str1=$data[$key]['fplace'];
					$str2=$data[$key]['place'];
					$map1['id']=$str1;
					$map2['id']=$str2;
					$data[$key]['tids']=M("District")->where($map1)->select();
					$data[$key]['tids1']=M("District")->where($map2)->select();
				}
				
				foreach ($data as $key => $value) {
					$str3=$data[$key]['labfid'];
					$str4=$data[$key]['labkid'];
					$map3['ID']=$str3;
					$map4['ID']=$str4;
					$data[$key]['tids2']=M("TbTeclab")->where($map3)->select();
					$data[$key]['tids3']=M("TbTeclab")->where($map4)->select();
				}
				
				foreach ($data as $key => $value) {
					$str=$data[$key]['tID'];					
					$mapp['id']=array('in',$str);					
					$data[$key]['tidss']=M("TbLable")->where($mapp)->select();				
				}
			//	$data['time']=date('Y-m-d');
			//	dump($data);
				$this->assign('data',$data);
			  $this->display();
			}
			
			
			public function  pipei(){								
					$id=I('id');
					$dem=M('TbDemand');	
                    $data = $dem->where("id={$id}")->find();
                    $this->assign('da',$data);
                    
				//	dump($data);
                    
                    $str=$dem->where('duijie_tb_demand.id='.$id)->getField('jsbq');
					$arr=explode(',', $str);
					
					$ach=M('TbTecach');
					$cou=$ach->count();
					$ar=array();
					for($i=1;$i<=$cou;$i++){
						
						$str2=$ach->where('duijie_tb_tecach.id='.$i)->getField('tID');
						$arr2=explode(',', $str2);
						$rt=array_intersect($arr,$arr2);

						if(empty($rt)==false)
						{
							
							$ar[]=$ach->where('duijie_tb_tecach.id='.$i)->getField('id');
						}
					}
					/*$str2=$ach->getField('tID');
					$arr2=explode(',', $str2);  */
					
					
				$id=I('id');
				$dem=M('TbDemand');	
				
				$tecach	=	M('TbTecach');
		//		$rt=isset($arr,$arr2);
//				dump($arr2);
//				$map['duijie_tb_tecach.tID']=array('exist',$str);
// 				$map['duijie_tb_tecach.tID']=explode(',', $str);
// 				= array(intval($str), array('like', '%' . $str . '%'), '_multi' => true);
// 				dump($map);
                $map['duijie_tb_tecach.id']=array('in',$ar);
				$list=$tecach->join('duijie_tb_teccoo on duijie_tb_tecach.hztype=duijie_tb_teccoo.cid')
				->join('duijie_tb_teccsd on duijie_tb_tecach.csd=duijie_tb_teccsd.sid')
				->field('duijie_tb_tecach.*,duijie_tb_teccsd.csd,duijie_tb_teccoo.cootype')
				->where($map)
				->select();
				//dump($list);
				foreach ($list as $key => $value) {
					$strr=$list[$key]['tID'];
					$mapp['id']=array('in',$strr);
					$list[$key]['tids']=M("TbLable")->where($mapp)->select();
				}
				
				foreach ($list as $key => $value) {
					$str3=$list[$key]['labfid'];
					$str4=$list[$key]['labkid'];
					$map1['ID']=$str3;
					$map2['ID']=$str4;
					$list[$key]['tids3']=M("TbTeclab")->where($map1)->select();
					$list[$key]['tids4']=M("TbTeclab")->where($map2)->select();
				}
				
				$this->assign('_list', $list);
				
				/* 专家信息 */
				$exp=M('TbExpert');
				$data=$exp->select();
				
				foreach ($data as $key => $value) {
					$st=$data[$key]['tid'];
					$map3['id']=array('in',$st);
					$data[$key]['tidss']=M("TbLable")->where($map3)->select();
				}
				
				foreach ($data as $key => $value) {
					$st1=$data[$key]['ffid'];
					$st2=$data[$key]['fid'];
					$map4['ID']=$st1;
					$map5['ID']=$st2;
					$data[$key]['tidss1']=M("TbTeclab")->where($map4)->select();
					$data[$key]['tidss2']=M("TbTeclab")->where($map5)->select();
				}
				
				$this->assign('data',$data);
				//dump($id);	
				
				$vip=M('TbVip');
				$rst=$vip->join('duijie_tb_demand on duijie_tb_vip.demid=duijie_tb_demand.id')
				->join('duijie_tb_tecach on duijie_tb_vip.achid=duijie_tb_tecach.id')
				->join('duijie_tb_expert on duijie_tb_vip.expid=duijie_tb_expert.eid')
				->select();
				//dump($rst);
				$this->assign('rst',$rst);
				
				$this->display();			
			
         }
         
         public function report(){
         	
         		if(IS_POST){
         			$id=I('id');
         			$dem=M('TbDemand');
         	
         			$rt=$dem->create();
         			if ($rt){
         				if($dem->save($rt)!==false){
         					$this->success(L('_SUCCESS_UPDATE_'),U('vipindex'));
         				}else {
         					$this->error(L('_FAIL_UPDATE_'));
         				}
         			}else {
         	
         				$this->error($dem->getError());
         				dump($rt);
         			}
         		}else {
         	
         			$demid=I('id');
         	
         			$vip=M('TbVip');
         			$map['duijie_tb_vip.demid']=$demid;
         			//	      		$data=$vip//->join('duijie_tb_demand on duijie_tb_demand.id=duijie_tb_vip.demid')
         			//	      		->join('duijie_tb_tecach on duijie_tb_tecach.id=duijie_tb_vip.achid','LEFT')
         			// 	      		->join('duijie_tb_expert on duijie_tb_expert.eid=duijie_tb_vip.expid')
         			//	      		->where($map)
         			//	      		->select();
         			$sql='select distinct achid,tecname from duijie_tb_vip,duijie_tb_tecach
	      				where duijie_tb_tecach.id=duijie_tb_vip.achid';
         			$data=$vip->where($map)->query($sql);
         			 
         			foreach ($data as $key => $value) {
         				$where['demid']=$demid;
         				$where['achid']=$value['achid'];
         				$data[$key]['zj']=$vip
         				->join('duijie_tb_expert on duijie_tb_expert.eid=duijie_tb_vip.expid')
         				->where($where)->select();
         				$data[$key]['cou']=count($data[$key]['zj']);
         			}
         	
         	
         			$rt=M('TbDemand')->find($demid);
         			$this->assign('rst',$rt);
         			//dump($rt);
         			//$this->assign('count',$cou);
         			$this->assign('data',$data);
         			$this->display();
         		}
         	
         }
         
         public function commit(){
         	if(IS_POST){
         		$id=I('id');
         		$dem=M('TbDemand');

         		$rt=$dem->create();
         		if ($rt){
	      			if($dem->save($rt)!==false){
	      				$this->success(L('_SUCCESS_UPDATE_'),U('vipindex'));
	      			    }else {
	      				    $this->error(L('_FAIL_UPDATE_'));
	      			}
	      		}else {
	      			
	      				$this->error($dem->getError());
	      				dump($rt);
	      			}
	      	}else {

	      		$demid=I('id');
	      		 
	      		$vip=M('TbVip');
	      		$map['duijie_tb_vip.demid']=$demid;
//	      		$data=$vip//->join('duijie_tb_demand on duijie_tb_demand.id=duijie_tb_vip.demid')
//	      		->join('duijie_tb_tecach on duijie_tb_tecach.id=duijie_tb_vip.achid','LEFT')
// 	      		->join('duijie_tb_expert on duijie_tb_expert.eid=duijie_tb_vip.expid')
//	      		->where($map)
//	      		->select();
	      		$sql='select distinct achid,tecname from duijie_tb_vip,duijie_tb_tecach 
	      				where duijie_tb_tecach.id=duijie_tb_vip.achid';
	      		$data=$vip->where($map)->query($sql);
	      		
 	      		foreach ($data as $key => $value) {
 	      			$where['demid']=$demid;
 	      			$where['achid']=$value['achid'];
 	      			$data[$key]['zj']=$vip
 	      			->join('duijie_tb_expert on duijie_tb_expert.eid=duijie_tb_vip.expid')
 	      			->where($where)->select();
 	      			$data[$key]['cou']=count($data[$key]['zj']);
 	      		}
 	      		
 	      		
                $rt=M('TbDemand')->find($demid);
                $this->assign('rst',$rt);
                //dump($rt);
	      		//$this->assign('count',$cou);
	      		$this->assign('data',$data);
	      		$this->display();
	      	}
         }
         
         public function pipeiing($id=''){
         	$this->display();
         	// $this->success('匹配完成',U('pipei?id='.$id),1);
         	$this->redirect('Actmanage/pipei', array('id' => $id), 3, ' ');
         }
         
         //项目列表
         public function xmindex(){
         	$auction	=	M('TbAuction');
         	$map['cID'] = UID;
         
         	$sql='SELECT * from duijie_tb_auction,duijie_tb_tecach
             WHERE duijie_tb_auction.tecID=duijie_tb_tecach.id';
         	$list=$auction->query($sql);
         		
         	int_to_string($list,array('jstatus'=>array(-1=>'不通过' ,0=>'申请中',1=>'进行中',2=>'已结束')));
         
         	$this->assign('_list', $list);
         	$this->display();
         }
         
         //项目详情
         public function jdetail($id=''){
         
         	$tecach = M(tb_tecach);
         
         	$where['duijie_tb_tecach.id']= $id;
         	$data = $tecach ->join('duijie_tb_techol on duijie_tb_techol.id = duijie_tb_tecach.cID')
         	->join('duijie_tb_teccoo on duijie_tb_teccoo.cid=duijie_tb_tecach.hztype')
         	->join('duijie_tb_teccsd on duijie_tb_teccsd.sid=duijie_tb_tecach.csd')
         	->join('duijie_tb_patent on duijie_tb_patent.id = duijie_tb_tecach.patentid')
         	->join('duijie_tb_award on duijie_tb_award.ID = duijie_tb_tecach.awardid')
         	->join('duijie_tb_paper on duijie_tb_paper.id = duijie_tb_tecach.paperid')
         	->where($where) ->select();
         
         	int_to_string($data,array('duijie'=>array(-1=>'未对接',0=>'对接中',1=>'对接已结束')));
         
         	foreach ($data as $key => $value) {
         		$str=$data[$key]['fplace'];
         		$str2=$data[$key]['place'];
         		$map['id']=$str;
         		$map2['id']=$str2;
         		$data[$key]['tids']=M("District")->where($map)->select();
         		$data[$key]['tids2']=M("District")->where($map2)->select();
         	}
         
         	foreach ($data as $key => $value) {
         		$str3=$data[$key]['ffield'];
         		$str4=$data[$key]['field'];
         		$map3['ID']=$str3;
         		$map4['ID']=$str4;
         		$data[$key]['tids3']=M("TbTeclab")->where($map3)->select();
         		$data[$key]['tids4']=M("TbTeclab")->where($map4)->select();
         	}
         
         	$this -> assign('data',$data);
         
         	$this->meta_title = '项目详情';
         	$this->display();
         }
         
         //通过
         public function jpass($id=''){
         	$pai = M(tb_auction);
         	$where['tecID'] =  $id;
         	$data['jstatus'] = '1';
         	$pai->where($where)->save($data);
         	$this->success('审核通过',U('index'));
         }
         
         //不通过
         public function jcancel($id=''){
         	$pai = M(tb_auction);
         	$where['tecID'] =  $id;
         	$data['jstatus'] = '-1';
         	$pai->where($where)->save($data);
         	$this->success('审核不通过',U('index'));
         		
         		
         }
          
       public function jindu(){
       	$this->display();
       }
       
       public function business(){
       	$coninf	=	M('TbConinf');
       	$tecname = I('tecname', '', 'text');
       	$map['status'] = array('egt', 0);
/*        	$map['cID'] = UID; */
       	if (is_numeric($tecname)) {
       		$map['id|tecname'] = array(intval($tecname), array('like', '%' . $tecname . '%'), '_multi' => true);
       	} else {
       		if ($tecname !== '') {
       			$map['tecname'] = array('like', '%' . (string)$tecname . '%');
       		}
       	}
       	
       	
       	/*  $list = $this->lists($coninf, $map);  */
       	$list=$coninf->join('duijie_tb_techol on duijie_tb_coninf.cid=duijie_tb_techol.id')
       	->join('duijie_tb_tecach on duijie_tb_coninf.jcid=duijie_tb_tecach.id')
       	->join('duijie_tb_tecdem on duijie_tb_coninf.jqid=duijie_tb_tecdem.id')
       	->select();
       	
       	int_to_string($list,array('htstatus'=>array(0=>'未付款',1=>'未付款',2=>'未付款',3=>'已交保证金',4=>'交易已结束')));              //数字转为字符串
       	
       	$this->assign('_list', $list);
       	$this->display();
       	
       	$this->display();
       }
       
}